package cn.xinfei.xdecision.data.core.vo.groovy;

import cn.xinfei.xdecision.common.model.datax.datamanage.v2.Variable;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;

@Getter
@Setter
@ToString
public class MixExpression {
    private BigInteger id;
    private String expression;
    transient private String variables;
    private List<Variable> variableList;


    public List<String> parseVariableIds(){
        if(StringUtils.isEmpty(variables)){
            return null;
        }
        return Arrays.asList(variables.split(","));
    }

    public void parseVariables(){
        if(null != variableList){
            StringBuffer string = new StringBuffer();
            for (Variable e :
                    variableList) {
                string.append(e.getId()).append(",");
            }
            setVariables(string.substring(0,string.length()-1));
        }
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof MixExpression)) {
            return false;
        }

        MixExpression that = (MixExpression) o;

        if (getId() != null ? !getId().equals(that.getId()) : that.getId() != null) {
            return false;
        }
        if (getExpression() != null) {
            return getExpression().equals(that.getExpression());
        }
        return that.getExpression() == null;
    }
}
